package com.fattyCorps.config.gameConfig;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fattyCorps.mapper.HeroMouldMapper;
import com.fattyCorps.obj.mould.HeroMould;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class HeroConfig implements CommandLineRunner {
    private List<HeroMould> heroMouldList = new ArrayList<>();

    private Integer allWeight = 0;

    public Integer getAllWeight() {
        return allWeight;
    }

    @Autowired
    HeroMouldMapper heroMouldMapper;

    public List<HeroMould> getHeroMouldList() {
        return heroMouldList;
    }

    public HeroMould getHeroMould(Integer typeId) {
        for (int i = 0; i < heroMouldList.size(); i++) {
            HeroMould heroMould = heroMouldList.get(i);
            if (heroMould.getId().equals(typeId)) {
                return heroMould;
            }
        }
        return null;
    }

    @Override
    public void run(String... args) throws Exception {
        // 加载配置
        //根据需求增加配置到heroMouldList
        HeroMould heroMould = new HeroMould();
        QueryWrapper<HeroMould> wrapper = new QueryWrapper<>();
        List<HeroMould> heroMoulds = heroMouldMapper.selectList(wrapper);

        //计算抽卡所需的英雄总权重值
        this.heroMouldList.addAll(heroMoulds);
        for (HeroMould mould : heroMouldList) {
            Integer weight = mould.getRollWeight();
            this.allWeight += weight;
        }
    }
}
